/***********************************************************************************
    Filename: hal_uart.c

    Copyright 2007 Texas Instruments, Inc.
***********************************************************************************/

#include "hal_types.h"
#include "hal_uart.h"
#include "hal_board.h"


//----------------------------------------------------------------------------------
//  void halUartInit(uint8 baudrate, uint8 options)
//----------------------------------------------------------------------------------
void halUartInit(uint8 baudrate, uint8 options)
{
    // For the moment, this UART implementation only
    // supports communication settings 115200 8N1
    // i.e. ignore baudrate and options arguments.

    UCA0CTL1 = UCSWRST;         // Keep USCI in reset state

    // Set up pins used by peripheral unit
    P3SEL |= BIT4 + BIT5;       // P3.4 = TXD, P3.5 = RXD

    UCA0CTL1 |= UCSSEL_2;       // SMCLK
    UCA0CTL0 = 0x00;            // 8-N-1
    UCA0BR0  = 0x68;            // 1MHz 9600
    UCA0BR1  = 0x00;            // 1MHz 9600
    UCA0MCTL = 0x02;            // 1Mhz Modulation
    UCA0CTL1 &= ~UCSWRST;       // Initialize USCI state machine
}

//----------------------------------------------------------------------------------
//  void halUartWrite(const uint8* buf, uint16 length)
//----------------------------------------------------------------------------------
void halUartWrite(const uint8* buf, uint16 length)
{
    uint16 i;
    for(i = 0; i < length; i++)
    {
        while (!(IFG2 & UCA0TXIFG));   // Wait for TX buffer ready to receive new byte
        UCA0TXBUF = buf[i];            // Output character
    }
}

//----------------------------------------------------------------------------------
//  void halUartRead(uint8* buf, uint16 length)
//----------------------------------------------------------------------------------
void halUartRead(uint8* buf, uint16 length)
{
    uint16 i;
    for(i = 0; i < length; i++)
    {
        while (!(IFG2 & UCA0RXIFG));   // Wait for new byte in RX buffer
        buf[i] = UCA0RXBUF;            // Output character
    }
}

